package com.zhihuichongcheng.application.ui.task;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.task.TaskMsgBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.task.adapter.TaskMsgAdapter;

import java.util.HashMap;
import java.util.List;

/**
 * Task message Created by LK on 2017/1/6.
 */
public class TaskMsgActivity extends BaseActivity implements View.OnClickListener {
    protected Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            }
        }
    };
    protected HttpManager manager;
    private XRecyclerView mRecyclerView;
    private TaskMsgAdapter mAdapter;
    private int currentPage = 1;
    private int pageCount = 20;
    private boolean hasNext = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_msg);
        manager = HttpManager.getInstance(this);
        initView();
        initListView();
    }

    private void initView() {
        setTitle("任务提醒");
        getTvArrowTitle().setVisibility(View.INVISIBLE);

        mRecyclerView = (XRecyclerView) this.findViewById(R.id.rv_task);
        mRecyclerView.setHasFixedSize(true);
    }

    private void initListView() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);

        mAdapter = new TaskMsgAdapter(this);
        mRecyclerView.setAdapter(mAdapter);


        //Header
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }

            @Override
            public void onLoadMore() {
                loadNextData();
            }
        });
    }

    private void refreshData() {
        if (null == app.getUser()) {
            ToastOpt.createToast(getContext(), "请先登录");
            return;
        }
        currentPage = 1;
        hasNext = true;
        mRecyclerView.setLoadingMoreEnabled(true);
        loadData();
    }

    private void loadNextData() {
        if (hasNext) {
            currentPage++;
            loadData();
        }
    }

    private void loadData() {
        DialogUtil.showLoadDiadlog(mContext);
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("curPage", currentPage + "");
        paramsDemo.put("pageRows", pageCount + "");
        //筛选条件
        manager.post(NetworkAPI.MISSION_MSG_LIST, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                List<TaskMsgBean> taskList = GsonUtil.paraeFromStringToList(value, TaskMsgBean.class);
                if (currentPage == 1)
                    mAdapter.reSetData(taskList);
                else
                    mAdapter.setData(taskList);
                mAdapter.notifyDataSetChanged();
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
                if (taskList.size() < pageCount) {
                    hasNext = false;
                    mRecyclerView.setLoadingMoreEnabled(false);
                }
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "查询失败 " + value);
                DialogUtil.dismiss(getContext());
                mRecyclerView.refreshComplete();
                mRecyclerView.loadMoreComplete();
            }
        }, paramsDemo);
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }
}
